package com.hlk.hlk_blog.service;


import com.hlk.hlk_blog.dao.TagMapper;
import com.hlk.hlk_blog.dao.TypeMapper;
import com.hlk.hlk_blog.entity.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by limi on 2017/10/16.
 */
@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;

    @Override
    public List<Tag> findTag(Integer tagNums) {
        return tagMapper.findTag(tagNums);
    }

    @Override
    public List<Tag> getAllTags() {
        return tagMapper.getAllTags();
    }

    @Override
    public Long getMaxTagId() {
        return tagMapper.getMaxTagId();
    }

    @Override
    public Tag getTagById(Long id) {
        return tagMapper.getTagById(id);
    }

    @Override
    public List<Tag> getTagByString(String text) {
        ArrayList<Tag> tags = new ArrayList<>();
        List<Long> longs = convertToList(text);
        for (Long long1 : longs) {
            tags.add(tagMapper.getTagById(long1));
        }
        return tags;
    }

    @Override
    public Tag getTagByName(String name) {
        return tagMapper.getTagByName(name);
    }

    @Override
    public boolean saveTag(Tag tag) {
        return tagMapper.saveTag(tag);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateTag(Tag tag) {
        return tagMapper.updateTag(tag);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteTag(Long id) {
        return tagMapper.deleteTag(id);
    }

    private List<Long> convertToList(String ids) {  //把前端的tagIds字符串转换为list集合
        List<Long> list = new ArrayList<>();
        if (!"".equals(ids) && ids != null) {
            String[] idarray = ids.split(",");
            for (int i = 0; i < idarray.length; i++) {
                list.add(new Long(idarray[i]));
            }
        }
        return list;
    }

}
